<?php

use App\Models\Plane\PlaneOrders\Repositories\Interfaces\PlaneOrderRepositoryInterface;
use App\Models\Train\TrainOrders\Repositories\Interfaces\TrainOrdersRepositoryInterface;
use Illuminate\Support\Facades\Broadcast;

/*
|--------------------------------------------------------------------------
| Broadcast Channels
|--------------------------------------------------------------------------
|
| Here you may register all of the event broadcasting channels that your
| application supports. The given channel authorization callbacks are
| used to check if an authenticated user can listen to the channel.
|
*/

Broadcast::channel('App.Models.User.{id}', function ($user, $id) {
    return (int)$user->id === (int)$id;
});

Broadcast::channel('train_orders.{order_sn}', function ($user, $order_sn) {
    Log::warning("train_orders.{order_sn}.{$order_sn}");
    $trainOrderRepo = app(TrainOrdersRepositoryInterface::class);
    $train_order    = $trainOrderRepo->getOrderInfo($order_sn);
    return $train_order->getUserId() == $user->getUserId();
});

Broadcast::channel('plane_orders.{order_sn}', function ($user, $order_sn) {
    Log::warning("plane_orders.{order_sn}.{$order_sn}");
    $planeOrderRepo = app(PlaneOrderRepositoryInterface::class);
    $plane_order    = $planeOrderRepo->getOrderInfo($order_sn);
    return $plane_order->getUserId() == $user->getUserId();
});
